package com.lanou.service.impl;

import com.lanou.bean.ShoppingBean;
import com.lanou.mapper.ShoppingOrdersMapper;
import com.lanou.service.ShoppingService;
import com.lanou.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/3/19.
 */
@Service
public class ShoppingServiceImpl implements ShoppingService{
    @Resource
    private ShoppingOrdersMapper shoppingOrdersMapper;

    /**
     * 由购物车生成订单
     * @param cartID
     * @param userID
     * @param payStatus
     * @return 返回给前端
     */
    @Override
    public AjaxResult shoppingOrders(@Param("cartID") Integer cartID,
                                     @Param("userID") Integer userID,
                                     @Param("payStatus") Integer payStatus) {
        AjaxResult ajaxResult = new AjaxResult();
        payStatus = payStatus == null ? 2 : payStatus;
        switch (payStatus){
            case 2:
                //待付款 修改状态为待付款
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,2);
                break;
            case 3:
                //已付款 修改状态为待发货
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,3);
                //查购买个数
                queryStockByCartID(cartID);
                break;
            case 4:
                //已发货 修改状态为待收货
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,4);
                break;
            case 5:
                //已收货 修改状态为待评价
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,5);
                break;
            case 6:
                //已评价 修改状态为已评价
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,6);
                break;
            case 7:
                //退款 修改状态为退款
                shoppingOrdersMapper.updateShoppingStatusByCartID(cartID,userID,7);
                break;
        }
        ajaxResult.setCode("true");
        ajaxResult.setMsg("成功");
        return ajaxResult;
    }

    /**
     * 由用户ID和订单状态动态查询
     * @param userID 用户ID
     * @param shoppingStatusID 订单状态(全部or待付款or待发货or待收货or待评价or退款)
     * @return json数据格式,
     */
    @Override
    public AjaxResult shoppingOrdersDetails(@Param("userID") Integer userID,
                                            @Param("shoppingStatus_id") Integer shoppingStatusID) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setData(shoppingOrdersMapper.queryShoppingOrdersByUserID(userID, shoppingStatusID));
        ajaxResult.setMsg("成功");
        ajaxResult.setCode("true");
        return ajaxResult;
    }

    /**
     * @desc 按cartID查购买数量
     * @param cartID
     */
    private void queryStockByCartID(Integer cartID) {
        ShoppingBean shoppingBean = shoppingOrdersMapper.queryStockByCartID(cartID);
        //减库存 加销量
        updateStockByCommodityID(shoppingBean.getCommodity_id(),shoppingBean.getCounts());
    }

    /**
     * 减库存,加销量
     * @param commodity_id
     * @param count
     */
    private void updateStockByCommodityID(Integer commodity_id,Integer count){
        shoppingOrdersMapper.updateStockByCommodityID(commodity_id,count);
    }
}
